package gui;

import java.awt.Color;
import java.awt.Font;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Rectangle;
import java.security.cert.X509Certificate;
import java.util.HashMap;

import javax.swing.JFileChooser;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.SwingConstants;

import code.KeyStoreManager;
import code.Language;
import code.Settings;

public class PersonalStoreCertificate extends JPanel {

	Color c= new Color(0x80, 0x80, 0xFF);
	
	private static final long serialVersionUID = 1L;
	private JLabel jLabel = null;

	private JLabel jLabel1 = null;

	private JTextField jTextField = null;

	private JLabel jLabel2 = null;

	private JButton jButton = null;

	private JLabel jLabel3 = null;
	private JLabel jLabel4 = null;

	ATOCOmain atocoframe;
	private KeyStoreManager ksm;
	private String filePath;
	
	String prevclassvalue;
	JPanel prevclass;
	
	private HashMap labels;
	Color generate = new Color(59, 89, 152);
	
	/**
	 * This is the default constructor
	 */
	public PersonalStoreCertificate(ATOCOmain atocoframe, JPanel prevclass, String prevclassvalue) {
		super();
		Settings s = new Settings();
		Language l = new Language();
		labels = l.changeLanguage((String) s.global.get("language"), "PersonalStoreCertificate", (String) s.global.get("installPath"));
		this.prevclass=prevclass;
		this.prevclassvalue=prevclassvalue;
		// Every Keystore operation must use this one.
		ksm = new KeyStoreManager(
				(String)Settings.global.get("personalKeyStoreFile"), 
				(String)Settings.global.get("personalKeyStorePassword"));
		if (ksm.getKeyStore() == null) {
        	System.out.println("Keystore file not found. Creating one.");
        	ksm.createKeyStore(
        			(String)Settings.global.get("personalKeyStoreFile"), 
    				(String)Settings.global.get("personalKeyStorePassword"));
        }
//		System.out.println("store certificate initialize oncesi");
		initialize();
//		System.out.println("store certificate initialize sonrasi");
		this.atocoframe=atocoframe;
//		System.out.println("StoreCertificate --> atocoframe: "+ this.atocoframe);
		
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		jLabel4 = new JLabel((String) labels.get("Store"), SwingConstants.CENTER);
		jLabel4.setBounds(new Rectangle(30, 231, 127, 39));
		jLabel4.setBackground(generate);
		jLabel4.setForeground(Color.WHITE);
		jLabel4.setOpaque(true);
		jLabel4.setFont(new Font("Calibri", Font.BOLD, 14));
		jLabel4.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent e) {
				store();
				setprevclassCert(jTextField.getText());
			}
		});
		jLabel3 = new JLabel();
		jLabel3.setBounds(new Rectangle(30, 181, 466, 16));
		jLabel3.setText((String) labels.get("Path"));
		jLabel3.setFont(new java.awt.Font("Dialog", Font.ITALIC, 13));
		//jLabel3.setForeground(c);
		jLabel2 = new JLabel();
		jLabel2.setBounds(new Rectangle(30, 106, 76, 16));
		jLabel2.setText((String) labels.get("Certificate"));
		jLabel2.setFont(new java.awt.Font("Dialog", Font.BOLD, 13));
		jLabel1 = new JLabel();
		jLabel1.setBounds(new Rectangle(30, 76, 61, 16));
		jLabel1.setText((String) labels.get("Alias"));
		jLabel1.setFont(new java.awt.Font("Dialog", Font.BOLD, 13));
		jLabel = new JLabel();
		jLabel.setBounds(new Rectangle(30, 31, 338, 16));
		jLabel.setText((String) labels.get("SelectCertificate"));
		jLabel.setForeground(generate);
		jLabel.setFont(new java.awt.Font("Dialog", Font.BOLD, 13));
		this.setSize(728, 447);
		this.setLayout(null);
//		System.out.println("add label once");
		this.add(jLabel, null);
		this.setBackground(Color.WHITE);
		this.add(jLabel1, null);
		this.add(getJTextField(), null);
		this.add(jLabel2, null);
		this.add(getJButton(), null);
		this.add(jLabel3, null);
		this.add(jLabel4, null);
		
//		System.out.println("add label sonra");
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setBounds(new Rectangle(120, 75, 150, 18));
		}
		return jTextField;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(120, 105, 150, 31));
			jButton.setText((String) labels.get("Select"));
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					browse();
				}
			});
		}
		return jButton;
	}
	
	public void browse() {
		JFileChooser fc = new JFileChooser(); 
		fc.setDialogTitle((String) labels.get("SelectFile"));
		fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
		fc.setMultiSelectionEnabled(true);
		fc.showOpenDialog(this);
		jLabel3.setText((String) labels.get("Path") + ": " + fc.getSelectedFile().getPath());
		filePath = fc.getSelectedFile().getPath();
	}
	
	public void store() {
		java.security.cert.Certificate cert = ksm.loadCertfromFileDer(filePath);
		ksm.saveCertificate(jTextField.getText(), (X509Certificate)cert);
		ksm.saveKeyStore();
	}
	
	private void setprevclassCert(String certAlias){
		if(prevclassvalue.equalsIgnoreCase("Verify")){
			VerifyDigitalSignature sign=(VerifyDigitalSignature)prevclass;
//			sign.setKeys((PublicKey)ksm.getPublicKey(keyAlias),
//					(PrivateKey)ksm.getPrivateKey(keyAlias, keyPassword));
			sign.setCert(ksm.getCertificate(certAlias));
			atocoframe.contentPane(sign);
		}
		else if(prevclassvalue.equalsIgnoreCase("Main")){
			CertificatePersonnel sign=(CertificatePersonnel)prevclass;
			atocoframe.contentPane(sign);
		}
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
